package com.gc.business.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Console;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gc.business.domain.*;
import com.gc.business.mapper.InventoryInfoMapper;
import com.gc.business.mapper.InventoryPriceMapper;
import com.gc.business.mapper.InventoryRecordMapper;
import com.gc.business.service.IInventoryPriceService;
import com.gc.common.utils.MoneyUtils;
import com.gc.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 库存规格中间 服务层实现
 * 
 * @author hruiyan
 * @date 2020-11-10
 */
@Slf4j
@Service
public class InventoryPriceServiceImpl extends ServiceImpl<InventoryPriceMapper, InventoryPrice> implements IInventoryPriceService {

    private final InventoryPriceMapper inventoryPriceMapper;

    private final InventoryInfoMapper inventoryInfoMapper;

    private final InventoryRecordMapper inventoryRecordMapper;

    @Autowired
    public InventoryPriceServiceImpl(InventoryPriceMapper inventoryPriceMapper,
                                     InventoryInfoMapper inventoryInfoMapper,
                                     InventoryRecordMapper inventoryRecordMapper) {
        this.inventoryPriceMapper = inventoryPriceMapper;
        this.inventoryInfoMapper = inventoryInfoMapper;
        this.inventoryRecordMapper = inventoryRecordMapper;
    }

    @Override
    public List<InventoryPrice> list(InventoryPrice inventoryPrice){
        LambdaQueryWrapper<InventoryPrice> wrapper = new LambdaQueryWrapper<>();
        if(inventoryPrice != null){
            if(StringUtils.isNotEmpty(inventoryPrice.getInventoryPriceId())){
                wrapper.eq(InventoryPrice::getInventoryPriceId, inventoryPrice.getInventoryPriceId());
            }
            if(StringUtils.isNotEmpty(inventoryPrice.getInventoryId())){
                wrapper.eq(InventoryPrice::getInventoryId, inventoryPrice.getInventoryId());
            }
            if(StringUtils.isNotEmpty(inventoryPrice.getPriceId())){
                wrapper.eq(InventoryPrice::getPriceId, inventoryPrice.getPriceId());
            }
            if(StringUtils.isNotEmpty(inventoryPrice.getItemNum())){
                wrapper.eq(InventoryPrice::getItemNum, inventoryPrice.getItemNum());
            }
            if(StringUtils.isNotEmpty(inventoryPrice.getItemSum())){
                wrapper.eq(InventoryPrice::getItemSum, inventoryPrice.getItemSum());
            }
            if(StringUtils.isNotEmpty(inventoryPrice.getCreateBy())){
                wrapper.eq(InventoryPrice::getCreateBy, inventoryPrice.getCreateBy());
            }
            if(StringUtils.isNotEmpty(inventoryPrice.getCreateTime())){
                wrapper.eq(InventoryPrice::getCreateTime, inventoryPrice.getCreateTime());
            }
            if(StringUtils.isNotEmpty(inventoryPrice.getUpdateBy())){
                wrapper.eq(InventoryPrice::getUpdateBy, inventoryPrice.getUpdateBy());
            }
            if(StringUtils.isNotEmpty(inventoryPrice.getUpdateTime())){
                wrapper.eq(InventoryPrice::getUpdateTime, inventoryPrice.getUpdateTime());
            }
            if(StringUtils.isNotEmpty(inventoryPrice.getRemark())){
                wrapper.eq(InventoryPrice::getRemark, inventoryPrice.getRemark());
            }
        }
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<InventoryPrice> getByInventoryId(Long inventoryId) {
        return baseMapper.getByInventoryId(inventoryId);
    }

    @Override
    @Transactional
    public void  addSave(InventoryPrice inventoryPrice, InventorySize inventorySize, InventorySizePrice inventorySizePrice) {

        inventoryPrice.setSizeName(inventorySize.getSizeTitle());
        inventoryPrice.setPriceName(inventorySizePrice.getPriceInfo());
        inventoryPriceMapper.insert(inventoryPrice);

        //获取库存数据
        InventoryInfo inventoryInfo = inventoryInfoMapper.selectById(inventoryPrice.getInventoryId());
        //修改库存数量和金额
        List<InventoryPrice> inventoryPriceList = inventoryPriceMapper.getByInventoryId(inventoryInfo.getInventoryId());
        String itemNumAll = "0";
        String itemSumAll = "0";
        for (InventoryPrice price : inventoryPriceList) {
            itemNumAll = MoneyUtils.moneyAdd(itemNumAll, price.getItemNum());
            //单个规格的所有数量价格
            String itemSumAllItem = MoneyUtils.moneyMul(price.getItemNum(), price.getItemSum());
            itemSumAll = MoneyUtils.moneyAdd(itemSumAll,itemSumAllItem);
        }
        inventoryInfo.setItemNum(itemNumAll);//总库存
        inventoryInfo.setItemSum(itemSumAll);//总金额
        inventoryInfo.setOperationTime(new DateTime());
        Console.log("itemNum:{},itemSum:{},operationTime:{}",inventoryInfo.getItemNum(),inventoryInfo.getItemSum(),inventoryInfo.getOperationTime());
        inventoryInfoMapper.updateById(inventoryInfo);

        //入库记录
        InventoryRecord inventoryRecord = new InventoryRecord();
        inventoryRecord.setInventoryId(inventoryInfo.getInventoryId());
        //todo
        //inventoryRecord.setInventoryPriceId(inventoryPrice.getInventoryPriceId());
        inventoryRecord.setRecordType("1");
        inventoryRecord.setOrderNo(inventoryInfo.getOrderNo());
        inventoryRecord.setSizeId(inventoryPrice.getSizeId());
        inventoryRecord.setSizeName(inventoryPrice.getSizeName());
        inventoryRecord.setPriceId(inventoryPrice.getPriceId());
        inventoryRecord.setPriceName(inventoryPrice.getPriceName());
        inventoryRecord.setItemName(inventoryInfo.getItemName());
        inventoryRecord.setItemNum(inventoryPrice.getItemNum());
        inventoryRecord.setRemainNum(inventoryPrice.getItemNum());
        inventoryRecordMapper.insert(inventoryRecord);

        inventoryPrice.setRecordId(inventoryRecord.getRecordId());
        inventoryPriceMapper.updateById(inventoryPrice);
    }

    @Override
    @Transactional
    public void removeAndSub(Long[] longIds) {
        //删除规格数据,修改总库存及总金额
        for (Long inventoryPriceId : longIds) {
            InventoryPrice inventoryPrice = inventoryPriceMapper.selectById(inventoryPriceId);
            InventoryInfo inventoryInfo = inventoryInfoMapper.selectById(inventoryPrice.getInventoryId());
            if (inventoryInfo == null){ continue; }
            inventoryInfo.setItemNum(MoneyUtils.moneySub(inventoryInfo.getItemNum(),inventoryPrice.getItemNum()));
            //单个规格的所有数量价格
            String itemSumAllItem = MoneyUtils.moneyMul(inventoryPrice.getItemNum(), inventoryPrice.getItemSum());
            inventoryInfo.setItemSum(MoneyUtils.moneySub(inventoryInfo.getItemSum(),itemSumAllItem));
            inventoryInfoMapper.updateById(inventoryInfo);
            //删除入库记录
            inventoryRecordMapper.deleteById(inventoryPrice.getRecordId());
            inventoryPriceMapper.deleteById(inventoryPriceId);
        }
    }
}
